﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YiLe.UI;

namespace YiLe.UI
{
    public partial class DataForm : Form
    {
        private DataFormEditMode mEditMode;
		public DataFormEditMode EditMode
		{
			get
			{
				return mEditMode;
			}
		}
		private string mDataMember;

		public string DataMember
		{
			get
			{
				return mDataMember;
			}
			set
			{
				mDataMember = value;
			}
		}
		public YiLe.UI.YiLeSystemData DataSet
		{
			get
			{
				return mDataSet;
			}
		}
		private string mDataProviderTypeName;
		//intended to be set by inheritors. If a class that inherits from
		//DataForm does not set this property, or the type name is not properly set an exception will be thrown
		//when trying to update data
		public string DataProviderTypeName
		{
			get
			{
				return mDataProviderTypeName;
			}
			set
			{
				mDataProviderTypeName = value;
			}
		}
		public void Edit(DataRow row)
		{
			/*//TODO: REMOVE ALL DATA INFO 
             * if ((mDataMember == null) || mDataMember.Length == 0)
			{
				throw new Exception("DataMember is not specified in the control");
			}
			if (row != null)
			{
				//create a copy of the datarow and import it to the dataset in the form

				//find the dataTable in the dataset owned by the form:
				DataTable table = this.DataSet.Tables[row.Table.TableName];
				//now add the row to the table:
				table.ImportRow(row);
				table.AcceptChanges();
				mEditMode = DataFormEditMode.Edit;
				CurrencyManager.Position = 0;
			}
			else
			{
				mEditMode = DataFormEditMode.AddNew;
				CurrencyManager.AddNew();
				this.btnDelete.Visible = false;
			}
			StartEdition();*/
			Show();
		}
		//Inheritors must override this method to set the databindings in its controls
		protected virtual void StartEdition()
		{

		}


		protected CurrencyManager CurrencyManager
		{
			get
			{
				return (CurrencyManager)BindingContext[DataSet, mDataMember];
			}
		}
		public DataRowView DataRowView
		{
			get
			{
				object o = CurrencyManager.Current;
				if (o is DataRowView)
				{
					return (DataRowView)o;
				}
				else
				{
					return null;
				}
			}
		}
		[System.ComponentModel.BrowsableAttribute(false)]
		public bool HasChanges
		{
			get
			{
				//if control is in design mode, return false:
				if (this.DesignMode)
				{
					return false;
				}
				if (CurrencyManager.Position == -1)
				{
					return false;
				}
				//get the current DataRowView
				DataRowView currentRow = (DataRowView)CurrencyManager.Current;
				if (mEditMode == DataFormEditMode.AddNew)
				{
					//if we are adding a row, check that at least one field
					//is different from its default.
					foreach (DataColumn column in currentRow.Row.Table.Columns)
					{
						if (currentRow[column.ColumnName] != column.DefaultValue)
						{
							return true;
						}
					}
					return false;
				}
				else
				{
					return currentRow.IsEdit;
				}
			}
		}
		protected virtual bool ValidateData()
		{
			return true;
		}
		public bool UpdateChanges()
		{
			if (! ValidateData())
			{
				return false;
			}
			try
			{
				CurrencyManager.EndCurrentEdit();
				Type providerType = Type.GetType(this.mDataProviderTypeName, true, true);
				DataProvider provider = YiLeSystemApp.GetDataProvider(providerType);
				return provider.UpdateDataRow(((DataRowView)CurrencyManager.Current).Row);
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.Message, YiLeSystemApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
		}
		protected override void OnLoad(System.EventArgs e)
		{

			base.OnLoad(e);
			//add all janus controls to the VisualStyleManager used in MainForm
			if (YiLeSystemApp.MainForm != null)
			{
                //TODO:VisualStyleManager1.AddControl()
				//YiLeSystemApp.MainForm.VisualStyleManager1.AddControl(this, true);
			}

		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			base.OnClosing(e);
			if (! e.Cancel && ! Commited && this.HasChanges)
			{
				DialogResult result = MessageBox.Show("Do you want to save changes?", YiLeSystemApp.MessageCaption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
				switch (result)
				{
					case System.Windows.Forms.DialogResult.Yes:
						if (! UpdateChanges())
						{
							e.Cancel = true;
						}
						break;
					case System.Windows.Forms.DialogResult.No:
						CurrencyManager.CancelCurrentEdit();
						break;
					case System.Windows.Forms.DialogResult.Cancel:
						e.Cancel = true;
						break;
				}
			}
		}


		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.CurrencyManager.CancelCurrentEdit();
			this.Close();
		}
		private bool Commited;
		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if (this.UpdateChanges())
			{
				this.Commited = true;
				this.Close();
			}
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			Type providerType = Type.GetType(this.mDataProviderTypeName, true, true);
			DataProvider provider = YiLeSystemApp.GetDataProvider(providerType);
			if (provider.DeleteDataRow(((DataRowView)CurrencyManager.Current).Row))
			{
				this.Commited = true;
				this.Close();
			}
		}
	}

	public enum DataFormEditMode: int
	{
		Edit,
		AddNew
	}

}
